package com.controller;

import com.Service.MenuService;
import com.model.pojo.Medical;
import com.model.pojo.Menu;
import com.model.pojo.UserPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller

@RequestMapping("menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    @Resource
    private Menu menu;


    @RequestMapping("index")
    public String index(HttpSession session, Model model, String show) {
        if (show != null) {

            List<Menu> menu = menuService.menu();//获取当前一级菜单 不可重复
            List<Menu> menucopy = menuService.menuCopy();//获取一级菜单备份版本 不可重复
            List<Menu> menuChild = menuService.menuChild();//获取二级菜单
            List<Menu> menuChildcopy = menuService.menuChildcopy();//获取二级菜单 备份版本  防止页面下拉框可选值删除后的菜单所影响

            session.setAttribute("menu", menu);
            session.setAttribute("menucopy", menucopy);
            session.setAttribute("menuChild", menuChild);
            session.setAttribute("menuChildcopy", menuChildcopy);
            return "menu";
        } else {

            return "test";
        }
    }


    @RequestMapping("modifyReceiving")
    public String modifyReceiving(HttpServletRequest request) {
        List<Menu> list = new ArrayList<Menu>();
        String[] onemenu = request.getParameterValues("onemenu");//获取修改后的一级菜单  二级菜单 和 地址
        String[] twomenu = request.getParameterValues("twomenu");
        String[] pathmenu = request.getParameterValues("pathmenu");


        for (int i = 0; i < onemenu.length; i++) {
            menu = new Menu();
            menu.setMenuNameOne(onemenu[i]);
            menu.setMenuNametwo(twomenu[i]);
            menu.setMenuPath(pathmenu[i]);


            list.add(menu);
        }


        boolean bl = menuService.modifyReceiving(list);


        if (bl == true) {


            return "redirect:/UserPermissions/index";

        } else {


            return "redirect:/UserPermissions/index";
        }


    }


}
